<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Booking controllers
 *
 * @author	Tran Van Thanh
 * @mail	thanhtran@vietnambiz.com
 * @date	07.09.2011
 */
require_once('admin'.EXT);

class Contacts extends Admin {

	function __construct()
	{
		parent::__construct();
		$this->load->model(array('contacts_model','booking_model'));
		$this->load->helper(array('text'));					
	}

	function index()
	{
		$contacts = $this->db->select()->from("contacts")->get()->result();
        $this->data['contacts'] =   $contacts;
			$this->data['tpl_file']	= 'contacts/index';
			$this->load->view('layout/default', $this->data);
	}
	
	function view($booking_id = null)
	{		
		$booking_info = $this->contacts_model->get_contacts_info($booking_id);		
		$this->data['booking'] 	= $booking_info;		
		$this->load->view('contacts/view', $this->data);
	}

    function delete($booking_id = null)
    {    	
        if($_SERVER['REQUEST_METHOD'] == 'POST') 
        {        	
            $this->contacts_model->delete_contacts($booking_id);         
            die('yes');
        }
    }
    
    function update($id = null)
    {
        $row  =   $this->db->select()->from("contacts")->where('id',$id)->get()->row();
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $_data = array(
                'name' => $this->input->post('name'),
                'yahoo' => $this->input->post('yahoo'),
                'email' => $this->input->post('email'),
                'tel' => $this->input->post('tel')       
            );
            if($row)
            {
                $_data['date_create']   =   date('Y-m-d H:i:s');
                $this->db->where('id',$row->id);
                $this->db->update('contacts',$_data);
            }
            else
            {
                 $this->db->insert('contacts',$_data);
            }
            redirect(admin_url('contacts'));
        }
        $this->data['row'] = $row;
        $this->data['tpl_file']	= 'contacts/update';
        $this->load->view('layout/default', $this->data);
    }

	function do_action()
	{
		if($_SERVER['REQUEST_METHOD'] == 'POST') {

			$id_list = $this->input->post('id');
			$action = $this->input->post('action');

			if($action == 'delete') {
				$this->delete($id_list);
			}				

			die('yes');
		}
	}

	function load_row($booking_id = null)
	{		
		$this->data['booking'] = $this->contacts_model->get($booking_id);		
		$this->load->view('contacts/row', $this->data);
	}	
}

